#include <iostream>
#include <cassert>
#include <string>
using namespace std;
namespace xzq
{
    template <class T>
    class vector
    {
    public:
        typedef T *iterator;
        typedef const T *const_iterator;
        iterator begin()
        {
            return _start;
        }
        iterator end()
        {
            return _finish;
        }
        const_iterator begin() const
        {
            return _start;
        }
        const_iterator end() const
        {
            return _finish;
        }
        vector()
            : _start(nullptr), _finish(nullptr), _endofstorage(nullptr)
        {
        }
        vector(const vector<T> &v)
            : _start(nullptr), _finish(nullptr), _endofstorage(nullptr)
        {
            reserve(v.capacity());
            for (const auto &e : v)
            {
                push_back(e);
            }
        }
        void swap(vector<T> &v)
        {
            ::swap(_start, v._start);
            ::swap(_finish, v._finish);
            ::swap(_endofstorage, v._endofstorage);
        }
        vector<T> &operator=(vector<T> v)
        {
            swap(v);
            return *this;
        }
        ~vector()
        {
            delete[] _start;
            _start = _finish = _endofstorage = nullptr;
        }
        void reserve(size_t n)
        {
            if (n > capacity())
            {
                size_t sz = size();
                T *tmp = new T[n];
                if (_start)
                {
                    for (size_t i = 0; i < sz; i++)
                    {
                        tmp[i] = _start[i];
                    }
                    delete[] _start;
                }
                _start = tmp;
                _finish = tmp + sz;
                _endofstorage = tmp + n;
            }
        }
        void insert(iterator pos, const T &x)
        {
            assert(pos <= _finish);
            if (_finish == _endofstorage)
            {
                size_t n = pos - _start;
                size_t newcapacity = capacity() == 0 ? 2 : 2 * capacity();
                reserve(newcapacity);
                pos = _start + n;
            }
            iterator end = _finish - 1;
            while (end >= pos)
            {
                *(end + 1) = *end;
                --end;
            }
            *pos = x;
            ++_finish;
        }
        iterator erase(iterator pos)
        {
            assert(pos < _finish);
            iterator it = pos;
            while (it < _finish)
            {
                *it = *(it + 1);
                ++it;
            }
            --_finish;
            return pos;
        }
        void push_back(const T &x)
        {
            insert(_finish, x);
        }
        void pop_back()
        {
            erase(_finish - 1);
        }
        void resize(size_t n, const T &val = T())
        {
            if (n < size())
            {
                _finish = _start + n;
            }
            else
            {
                if (n > capacity())
                {
                    reserve(n);
                }
                while (_finish < _start + n)
                {
                    *_finish = val;
                    ++_finish;
                }
            }
        }
        size_t size() const
        {
            return _finish - _start;
        }
        size_t capacity() const
        {
            return _endofstorage - _start;
        }
        T &operator[](size_t i)
        {
            assert(i < size());
            return _start[i];
        }
        const T &operator[](size_t i) const
        {
            assert(i < size());
            return _start[i];
        }

    private:
        iterator _start;
        iterator _finish;
        iterator _endofstorage;
    };
    void print_vector(const vector<int> &v)
    {
        vector<int>::const_iterator it = v.begin();
        while (it != v.end())
        {
            std::cout << *it << " ";
            ++it;
        }
        std::cout << std::endl;
    }
    void test()
    {
        vector<std::string> v;
        v.push_back("111111111");
        v.push_back("222222222");
        v.push_back("444444444");
        v.push_back("555555555");
        v.push_back("666666666");
        for (auto e : v)
        {
            std::cout << e << " ";
        }
        std::cout << std::endl;
    }
    void test1()
    {
        vector<int> v;
        v.push_back(1);
        v.push_back(2);
        v.push_back(3);
        v.push_back(4);
        //vector<int>::iterator it = v.begin();
        // v.insert(it + 2,8);
        //v.erase(it + 3);
        v.resize(20,5);
        v.reserve(35);
        print_vector(v);
        std::cout<<v.size()<<std::endl;
        std::cout<<v.capacity()<<std::endl;
        vector<int> v1(v);
        print_vector(v1);
        std::cout<<v1.size()<<std::endl;
        std::cout<<v1.capacity()<<std::endl;
        vector<int> v2;
        v2.push_back(11);
        v2.push_back(22);
        v2.push_back(33);
        v2.push_back(44);
        print_vector(v2);
        std::cout<<v2.size()<<std::endl;
        std::cout<<v2.capacity()<<std::endl;
        v1 = v2;
        print_vector(v1);
    }
}